package com.example.finalassignment.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.example.finalassignment.common.Cache;
import com.example.finalassignment.common.ThreadContext;
import com.example.finalassignment.exception.ServiceException;
import com.example.finalassignment.mapper.UserMapper;
import com.example.finalassignment.pojo.User;
import com.example.finalassignment.pojo.dto.UserLoginDto;
import com.example.finalassignment.pojo.dto.UserRegisterDto;
import com.example.finalassignment.pojo.dto.UserUpdateDto;
import com.example.finalassignment.pojo.response.LoginResponse;
import com.example.finalassignment.pojo.response.ResultResponse;
import com.example.finalassignment.service.UserService;
import com.example.finalassignment.util.GenerateUtil;
import com.example.finalassignment.util.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

    private final UserMapper userMapper;

    public UserController(UserService userService, UserMapper userMapper) {
        this.userService = userService;
        this.userMapper = userMapper;
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public ResultResponse login(@RequestBody UserLoginDto userLoginDto) {

//        System.out.println(userLoginDto);
        return userService.login(userLoginDto);
    }

    /**
     * 根据token登录
     */
    @GetMapping("/token")
    public ResultResponse loginByToken(){
        return userService.loginByToken();
    }
    /**
     * 注册
     */
    @PostMapping("/register")
    public ResultResponse register(@RequestBody UserRegisterDto userRegisterDto){
        System.out.println(userRegisterDto);
        return userService.register(userRegisterDto);
    }

    @GetMapping("/logout")
    public ResultResponse logout() {
        String token = ThreadContext.getToken();
        if (token != null) {
             Cache.USER_INFO.remove(token);
             return ResultResponse.success("退出成功");
        }
        return ResultResponse.fail(200,"退出失败");
    }

    /**
     * 修改用户信息
     */
    @PostMapping("/update")
    public ResultResponse updateByUserInfo(@RequestBody UserUpdateDto userUpdateDto) {
        return userService.updateByUserInfo(userUpdateDto);
    }

    /**
     * 修改密码
     */
    @GetMapping("/password")
    public ResultResponse changePassword(String password){
        if (password == null || password.isEmpty()){
            return ResultResponse.fail(200,"密码不能为空");
        }

        String id = JwtUtil.getIdByToken(ThreadContext.getToken());
        password = GenerateUtil.StringByMd5(password);
        try {
            userMapper.updateByPassWordToUserId(id, password);
            return ResultResponse.success("修改成功");
        }catch (Exception e){
            throw new ServiceException(500,"修改失败");
        }

    }
}
